var https = require('https');
var cheerio = require('cheerio');
var fs = require('fs');
var query = require('querystring');

var searchWord = '';
var searchResult = {
    name:'',
    url:'',
    des:{},
    items:[]
}

// getDepartment('other');

// subURL(searchWord);

function getDepartment(fileName){
    var depFS = fs.readFileSync('./source/'+fileName+'.json');
    var depJson = JSON.parse(depFS);
    searchWord = depJson.name;
    subURL(searchWord);
    depJson.items.forEach(element => {
        subURL(element);
    });
}

function subURL(keyWord) {
    //搜索
    var url = 'https://baike.baidu.com/search/word?'+query.stringify({word : keyWord});
    getBaiKeHtml(url,keyWord,finish);
}

function getBaiKeHtml(url, word, callback){
    https.get(url, (response) => {
        var html = '';
        response.on('data', (tempData) => {
            html += tempData;
        })
        var code = response.statusCode;
        response.on('end', () => {
            if(code == 200) {
                //正常，callback
                callback(url,word,html);
            }else if (code == 302) {
                //异常重新定向
                var location = response.headers.location;
                if(location.indexOf('//baike.baidu.com') >= 0){
                    location = 'https:'+location;
                    getBaiKeHtml(location,word,callback);
                }else if(location.indexOf('/item/') >= 0){
                    location = 'https://baike.baidu.com'+location;
                    getBaiKeHtml(location,word,callback);
                }else{
                    console.log('出现异常暂时不予分析',response.statusCode,response.headers);
                }
            }
        });
    }).on('error', (error) => {
        //请求失败;
        console.log('失败：暂时不予分析');
    });
}

function finish(url,word,html){
    var attributes = filterHtml(html);
    if(word == searchWord){
        searchResult.name = word;
        searchResult.url = url;
        searchResult.des = attributes;
    }else{
        var content = {
            name:word,
            url:url,
            des:attributes
        };
        searchResult.items.push(content);
    }
    saveHtml(word,html);
    saveJson(searchWord,JSON.stringify(searchResult).replace(/\\n+/g,''));
}

function filterHtml(html){
    if(html){
        var attributes = [];
        var $ = cheerio.load(html);
        var basicInfo = $('div.basic-info dl');
        basicInfo.each(function(){
            var item = $(this).find('.basicInfo-item');
            var key = '';
            var value = '';
            let num =1;
            item.each(function() {
                var node = $(this);
                var nodeClass = node.attr('class');
                if(nodeClass == "basicInfo-item name"){
                    //名称
                    key = node.text();
                }else{
                    //值
                    value = node.text();
                }
                if(num%2 == 0){
                    var obj = {
                        name:key,
                        value:value
                    }
                    attributes.push(obj);
                    key = '';
                    value = '';
                }
                num++;
            });            
        });
        return attributes;
    }
    return {};
}

function saveJson(key,json){
    saveFile('./result/json/'+key+'.json',json);
}
function saveHtml(key,html){
    saveFile('./result/html/'+key+'.html',html)
    console.log('保存文件');
}

function saveFile(filePath,content){
    console.log('保存文件');
    fs.writeFileSync(filePath, content, (e) => {
        if (e) throw e;
        console.log('It\'s saved!');
    });
}